"""
功能描述：单独处理请求的参数配置，和请求方法的判断，以及响应结果的提取
导包
创建类
    实例化属性
    1-接受testcase传入的请求参数
        id, interfaceUrl, name, method, value, expect, real, status
    2-根据接口的请求方式进行判断
            get--requests.get
            post--requests.post
    3-返回接口执行后的结果
"""

# 导包
from 高级版接口第一周.common.log import logger
import requests

# 创建类
class confighttp():
#     实例化属性
    def __init__(self,interfaceUrl,method,value,header):
        # 1-接受testcase传入的请求参数
        # id, interfaceUrl, name, method, value, expect, real, status
        self.interfaceUrl=interfaceUrl
        self.method=method
        self.value=eval(value)
        self.header=header
        #2-根据接口的请求方式进行判断
        # get--requests.get
        # post--requests.post
        # 3-返回接口执行后的结果
    def run(self):
        """
           对外提供执行请求的方法
           :return:
        """
        if self.method.lower()=='get':
            return self.__get()
        elif self.method.lower()=='port':
            return self.__post()
        elif self.method.lower()=='put':
            return self.__put()
        elif self.method.lower()=='delete':
            return self.__delete()
        else:
            print('未找到匹配数据，请检查')

    def __get(self):
        try:
            re=requests.get(url=self.interfaceUrl,params=eval(self.value),headers=self.header)
            logger.info(f'接口返回：{re.text}')
        except Exception as msg:
            print('get接口请求失败，系统提示：',msg)
        else:
            return re
    def __post(self):
        try:
            re=requests.post(url=self.interfaceUrl,data=eval(self.value),headers=self.header)
            logger.info(f'post接口返回接口返回：{re.text}')
        except Exception as msg:
            print('post接口返回接口请求失败，系统提示：',msg)
        else:
            return re

    def __put(self):
        try:
            re = requests.put(url=self.interfaceUrl , data=eval(self.value),headers = eval(self.header))
            logger.info(f'接口返回：{re.text}')
        except Exception as msg:
            print('接口请求失败，系统提示：',msg)
        else:
            return re

    def __delete(self):
        try:
            re=requests.put(url=self.interfaceUrl,data=eval(self.value),headers=eval(self.header))
            logger.info(f'接口请求返回:{re.text}')
        except Exception as msg:
            print("接口请求失败，系统提示：" ,msg)
        else:
            return re

if __name__ == '__main__':
    url = ''
    method = 'post'
    value = "{'username':'liangchao','password':'123456'}"
    header = "{}"
    con=confighttp(url,method,value,header)
    con.run()